package cn.edu.scnu.controller;

import cn.edu.scnu.Service.AddressService;
import cn.edu.scnu.entity.Address;
import cn.edu.scnu.entity.Return;
import cn.edu.scnu.entity.User;
import cn.edu.scnu.util.JwtUitl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class AddressController {
    @Autowired
    private AddressService addressService;
    @PostMapping("/addAddress")
    public Return userInfo(@RequestHeader(name="Authorization") String token, @RequestBody Address address){
        String username = (String) JwtUitl.parseToken(token).get("username");
        return addressService.addAddress(username, address);
    }

    @GetMapping("/findAllAddress")
    public Return findAllAddress(@RequestHeader(name="Authorization") String token){
        String username = (String) JwtUitl.parseToken(token).get("username");
        return addressService.findByUsername(username);
    }

    @PostMapping("/updateAddressById")
    public Return updateAddressById(@RequestBody Address address){
        return addressService.updateAddressById(address);
    }

    @GetMapping("/setDefaultById")
    public Return setDefaultById(@RequestParam String addressId){
        return addressService.setDefaultById(addressId);
    }

    @DeleteMapping("/deleteAddressById")
    public Return deleteAddressById(@RequestParam String addressId){
        return addressService.deleteAddressById(addressId);
    }

    @GetMapping("/findDefaultAddress")
    public Return findDefaultAddress(@RequestHeader(name="Authorization") String token){
        String username = (String) JwtUitl.parseToken(token).get("username");
        return addressService.findDefaultAddress(username);
    }
}
